package com.dm.shop.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dm.shop.common.enums.ResultEnum;
import com.dm.shop.entity.Category;
import com.dm.shop.entity.Product;
import com.dm.shop.service.CategoryService;
import com.dm.shop.service.ProductService;
import com.dm.shop.utils.ResultUtil;
import com.dm.shop.vo.ProductVO;
import com.dm.shop.vo.Result;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author MyBatisPlusGenerater-Saiya Wang
 * @since 2021-07-20
 */
@CrossOrigin
@RestController
@RequestMapping("/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    @Autowired
    private ProductService productService;

    @PostMapping("/all")
    @ApiOperation("获取所有类别")
    private Result getAllCategory()
    {
        //查询所有数据，条件为null
        List<Category> categoryList = categoryService.list(null);
        return ResultUtil.success(categoryList);
    }

    @GetMapping("/{cid}")
    @ApiOperation("根据类别id筛选商品")
    private Result getProductByCid(@PathVariable("cid") Integer cid)
    {
        QueryWrapper<Product> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("cid",cid);
        List<Product> productList = productService.list(queryWrapper);
        if (productList == null)
        {
            return ResultUtil.error(ResultEnum.FAIL);
        }
        System.out.println("查询到的内容："+productList);
        return ResultUtil.success(productList);
    }

}
